<html> 
<head> 
    <script src="../../lib/OpenLayers.js"></script> 
    <script type="text/javascript">

    function test_Comparison_constructor(t) { 
        t.plan(3); 
         
        var options = {'foo': 'bar'}; 
        var filter = new OpenLayers.Filter.Comparison(options); 
        t.ok(filter instanceof OpenLayers.Filter.Comparison, 
             "new OpenLayers.Filter.Comparison returns object" ); 
        t.eq(filter.foo, "bar", "constructor sets options correctly"); 
        t.eq(typeof filter.evaluate, "function", "filter has an evaluate function"); 
    }

    function test_Comparison_destroy(t) {
        t.plan(1);
        
        var filter = new OpenLayers.Filter.Comparison();
        filter.destroy();
        t.eq(filter.symbolizer, null, "symbolizer hash nulled properly");
    }
    
    function test_Comparison_value2regex(t) {
        t.plan(4);
        
        var filter = new OpenLayers.Filter.Comparison({
                property: "foo",
                value: "*b?r\\*\\?*",
                type: OpenLayers.Filter.Comparison.LIKE});
        filter.value2regex("*", "?", "\\");
        t.eq(filter.value, ".*b.r\\*\\?.*", "Regular expression generated correctly.");
        
        filter.value = "%b.r!%!.%";
        filter.value2regex("%", ".", "!");
        t.eq(filter.value, ".*b.r\\%\\..*", "Regular expression with different wildcard and escape chars generated correctly.");
    
        filter.value = "!!";
        filter.value2regex();
        t.eq(filter.value, "\\!", "!! successfully unescaped to \\!");
        
        // Big one.
        filter.value = "!!c!!!d!e";
        filter.value2regex();
        t.eq(filter.value, "\\!c\\!\\d\\e", "!!c!!!d!e successfully unescaped to \\!c\\!\\d\\e");
    }
    
    function test_regex2value(t) {
        t.plan(8);
        
        function r2v(regex) {
            return OpenLayers.Filter.Comparison.prototype.regex2value.call(
                {value: regex}
            );
        }
        
        t.eq(r2v("foo"), "foo", "doesn't change string without special chars");
        t.eq(r2v("foo.*foo"), "foo*foo", "wildCard replaced");
        t.eq(r2v("foo.foo"), "foo.foo", "singleChar replaced");
        t.eq(r2v("foo\\\\foo"), "foo\\foo", "escape removed");
        t.eq(r2v("foo!foo"), "foo!!foo", "escapes !");
        t.eq(r2v("foo\\*foo"), "foo!*foo", "replaces escape on *");
        t.eq(r2v("foo\\.foo"), "foo!.foo", "replaces escape on .");
        t.eq(r2v("foo\\\\.foo"), "foo\\.foo", "unescapes only \\ before .");
        
    }
    
    function test_Comparison_evaluate(t) {
        t.plan(5);
        
        var filter = new OpenLayers.Filter.Comparison({
                property: "area",
                lowerBoundary: 1000,
                upperBoundary: 4999,
                type: OpenLayers.Filter.Comparison.BETWEEN});

        var features = [
                new OpenLayers.Feature.Vector(null, {
                        area: 999}),
                new OpenLayers.Feature.Vector(null, {
                        area: 1000}),
                new OpenLayers.Feature.Vector(null, {
                        area: 4999}),
                new OpenLayers.Feature.Vector(null, {
                        area: 5000})];
        // PropertyIsBetween filter: lower and upper boundary are inclusive
        var filterResults = {
                0: false,
                1: true,
                2: true,
                3: false};
        for (var i in filterResults) {
            var result = filter.evaluate(features[i].attributes);
            t.eq(result, filterResults[i], "feature "+i+
                    " evaluates to "+result.toString()+" correctly.");
        }
        var context = {
            area: 4998
        };
        var result = filter.evaluate(context);
        t.eq(result, true, "evaluation against custom filter context works.");
    }
    </script> 
</head> 
<body> 
</body> 
</html> 
